Conditions | 4 |
Paths | 1 |
Total Lines | 57 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | 'use strict'; |
||
36 | login: co.wrap(function *(id, pwd) { |
||
37 | let ires, iires; |
||
38 | |||
39 | // 通过GET首页,来获取cookie |
||
40 | try { |
||
41 | ires = yield superagent |
||
42 | .get('http://csujwc.its.csu.edu.cn/jsxsd') |
||
43 | .endThunk(); |
||
44 | } catch (err) { |
||
45 | let message = { |
||
46 | inner: `Failed to get the Cookie for login.\n${err.stack}`, |
||
47 | public: '获取Cookie失败' |
||
48 | }; |
||
49 | throw message; |
||
50 | } |
||
51 | |||
52 | const headers = { |
||
53 | Host: 'csujwc.its.csu.edu.cn', |
||
54 | Connection: 'keep-alive', |
||
55 | 'Cache-Control': 'max-age=0', |
||
56 | Accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', |
||
57 | Origin: 'http://csujwc.its.csu.edu.cn', |
||
58 | 'Upgrade-Insecure-Requests': 1, |
||
59 | 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36', |
||
60 | 'Content-Type': 'application/x-www-form-urlencoded', |
||
61 | Referer: 'http://csujwc.its.csu.edu.cn/jsxsd/', |
||
62 | 'Accept-Encoding': 'gzip, deflate', |
||
63 | 'Accept-Language': 'zh-CN,zh;q=0.8', |
||
64 | Cookie: ires.headers['set-cookie'] |
||
65 | }; |
||
66 | |||
67 | try { |
||
68 | iires = yield superagent |
||
69 | .post('http://csujwc.its.csu.edu.cn/jsxsd/xk/LoginToXk') |
||
70 | .set(headers) |
||
71 | .type('form') |
||
72 | .send({ encoded: `${encodeInp(id)}%%%${encodeInp(pwd)}` }) |
||
73 | .endThunk(); |
||
74 | } catch (err) { |
||
75 | let message = { |
||
76 | inner: `Failed to login\n${err.stack}`, |
||
77 | public: '登录失败' |
||
78 | }; |
||
79 | throw message; |
||
80 | } |
||
81 | |||
82 | if (/POST/i.test(iires.req.method)) { |
||
83 | let err = new Error('The request method to the logged-in page is POST instead of GET'); |
||
84 | let message = { |
||
85 | inner: `Failed to login (possibily id or password provided were wrong)\n${err.stack}`, |
||
86 | public: '登录失败,可能是用户名或密码错误,请确认参数已URL转义' |
||
87 | }; |
||
88 | throw message; |
||
89 | } |
||
90 | |||
91 | return headers; |
||
92 | }), |
||
93 | // 登出模块 |
||
109 |